package com.ydc.ln.tutorial.j2se.ms;

/**
 * 循环条件 测试
 * 
 * @author ydc
 * @date 下午6:32:45 2013-7-12
 */
public class Ch0702 {

    static int stepCount = 0;

    public static void main(String[] args) {
        new Ch0702().test003();
        System.out.println("stepCount: " + stepCount);
    }

    
    /**
     * 筛选法求素数 void
     */
    public void test004() {
        int[] a = new int[101];
        for (int i = 0; i < 101; i++) {
            a[i] = 1;
        }
        for (int i = 2; i < 101; i++) {
            if (a[i] != 0) {
                for (int j = i + i; j < 101;) {
                    stepCount++;
                    System.out.println("stepCount=" + stepCount + "\ti=" + i
                            + "\tj=" + j);
                    if (j % i == 0) {
                        a[j] = 0;
                        j += i;
                    }
                }
            }
        }
        for (int i = 2; i < 101; i++) {
            if (a[i] != 0) {
                System.out.println(i);
            }
        }
    }

    /**
     * 100以内的素数 void 质数又称素数。指在一个大于1的自然数中，除了1和此整数自身外，不能被其他自然数整除的数
     */
    public void test003() {
        int n = 100;
        for (int i = 1; i <= n; i++) {
            if (isSushu(i)) {
                System.out.println(i);
            }
        }
    }

    public boolean isSushu(int n) {
        boolean result = true;
        int nTmp = (int) Math.sqrt(n);
        for (int i = 2; i <= nTmp; i++) {
            stepCount++;
            if (n % i == 0) {
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * 给一个整数n，求1到n之间这n个数中，1出现的次数 void
     */
    public void test002() {
        int n = 13;

        int oneTotal = 0;
        for (int i = 1; i <= n; i++) {
            oneTotal += test0021(i);
        }
        System.out.println(n + "以内中1的个数: " + oneTotal);
    }

    public int test0021(int n) {
        int result = 0;
        char[] nChars = String.valueOf(n).toCharArray();
        int nLen = nChars.length;
        for (int i = 0; i < nLen; i++) {
            if (1 == Character.getNumericValue(nChars[i])) {
                result++;
            }
        }
        return result;
    }

    public void test001() {
        for (int i = 0; i < 10; i++) {
            Integer k = new Integer(i);
        }
    }
}
